/* vim: set ft=cpp: */
%%
headers
#include <gtksourceview/gtksourceview.h>
#include <gtksourceview/gtksourcebuffer.h>
#include <gtksourceview/gtksourceiter.h>
#include <gtksourceview/gtksourcelanguage.h>
#include <gtksourceview/gtksourcelanguagesmanager.h>
#include <gtksourceview/gtksourcemarker.h>
#include <gtksourceview/gtksourcestylescheme.h>
#include <gtksourceview/gtksourcetag.h>
#include <gtksourceview/gtksourcetagstyle.h>
#include <gtksourceview/gtksourcetagtable.h>
#include <gtksourceview/gtksourceview-typebuiltins.h>
#include "ext/gtk+/php_gtk+.h"
#include "php_gtk_api.h"
%%
constants
%%
ignore-glob
  *_get_type

%% {{{ GtkSourceBuffer

%%
override gtk_source_buffer_get_escape_char
PHP_METHOD
{
    gunichar ichar;
    gint len;
    gchar *ret = safe_emalloc(6, sizeof(gchar *), 0);
    gchar *cp_ret;
    gsize cp_len;
    zend_bool free_result = FALSE;

    NOT_STATIC_METHOD();

    if (!php_gtk_parse_args(ZEND_NUM_ARGS(), ""))
        return;

    ichar = gtk_source_buffer_get_escape_char(GTK_SOURCE_BUFFER(PHPG_GOBJECT(this_ptr)));

    if (ichar) {
        len = g_unichar_to_utf8(ichar, ret);
        cp_ret = phpg_from_utf8(ret, len, &cp_len, &free_result TSRMLS_CC);

        if (cp_ret) {
            RETVAL_STRINGL((char *)cp_ret, cp_len, 1);
        } else {
            php_error(E_WARNING, "%s::%s(): could not convert return value from UTF-8", get_active_class_name(NULL TSRMLS_CC), get_active_function_name(TSRMLS_C));
        }

        if (free_result)
            g_free(cp_ret);
        else
            RETVAL_STRINGL((char *)ichar, len, 1);
    } else {
        RETVAL_NULL();
    }

    efree(ret);
}

%%
add-arginfo GtkSourceBuffer set_escape_char
PHPGTK_ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(ARGINFO_NAME, 0)
    ZEND_ARG_INFO(0, char)
ZEND_END_ARG_INFO();

%%
override gtk_source_buffer_set_escape_char
PHP_METHOD
{
    char *getchar;
    gunichar setchar;
    zend_bool free_char = FALSE;

    NOT_STATIC_METHOD();

    if (!php_gtk_parse_args(ZEND_NUM_ARGS(), "u", &getchar, &free_char))
        return;

    setchar = g_utf8_get_char((const gchar*)getchar);
    gtk_source_buffer_set_escape_char(GTK_SOURCE_BUFFER(PHPG_GOBJECT(this_ptr)), setchar);

    if (free_char)
        efree(getchar);
}

%% }}}
%% {{{ GtkSourceLanguagesManager

%%
override gtk_source_languages_manager_get_available_languages
PHP_METHOD
{
    const GSList *list;
    zval *item;

    NOT_STATIC_METHOD();

    if (!php_gtk_parse_args(ZEND_NUM_ARGS(), "")) {
        return;
    }

    array_init(return_value);
    for (list = gtk_source_languages_manager_get_available_languages(GTK_SOURCE_LANGUAGES_MANAGER(PHPG_GOBJECT(this_ptr))); list; list = list->next) {
        MAKE_STD_ZVAL(item);
        phpg_gobject_new(&item, G_OBJECT(list->data) TSRMLS_CC);
        add_next_index_zval(return_value, item);
    }
}

%%
add-arginfo GtkSourceLanguagesManager get_lang_files_dirs
PHPGTK_ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO_EX(ARGINFO_NAME, 0, 0, 0)
    ZEND_ARG_INFO(0, convert)
ZEND_END_ARG_INFO();

%%
override gtk_source_languages_manager_get_lang_files_dirs
PHP_METHOD
{
    const GSList *list;
    gchar *fn = NULL;
    gchar *cp = NULL;
    gsize cp_len = 0;
    zend_bool free_cp = FALSE;
    zend_bool convert = 1;

    NOT_STATIC_METHOD();

    if (!php_gtk_parse_args(ZEND_NUM_ARGS(), "|b", &convert)) {
        return;
    }

    array_init(return_value);
    for (list = gtk_source_languages_manager_get_lang_files_dirs(GTK_SOURCE_LANGUAGES_MANAGER(PHPG_GOBJECT(this_ptr))); list; list = list->next) {
        if (convert) {
            fn = g_filename_to_utf8((char *) list->data, strlen((char *) list->data), NULL, NULL, NULL);
            cp = phpg_from_utf8(fn, strlen(fn), &cp_len, &free_cp TSRMLS_CC);
            if (cp) {
                add_next_index_string(return_value, (char *)cp, 1);
            } else {
                php_error(E_WARNING, "%s::%s(): could not convert return value from UTF-8", get_active_class_name(NULL TSRMLS_CC), get_active_function_name(TSRMLS_C));
            }
            if (free_cp)
                g_free(cp);
        } else {
            add_next_index_string(return_value, (char *) list->data, 1);
        }
    }
}

%% }}}
%% {{{ GtkSourceLanguage

%%
override gtk_source_language_get_escape_char
PHP_METHOD
{
    gunichar ichar;
    gint len;
    gchar *ret = safe_emalloc(6, sizeof(gchar *), 0);
    gchar *cp_ret;
    gsize cp_len;
    zend_bool free_result = FALSE;

    NOT_STATIC_METHOD();

    if (!php_gtk_parse_args(ZEND_NUM_ARGS(), ""))
        return;

    ichar = gtk_source_language_get_escape_char(GTK_SOURCE_LANGUAGE(PHPG_GOBJECT(this_ptr)));

    if (ichar) {
        len = g_unichar_to_utf8(ichar, ret);
        cp_ret = phpg_from_utf8(ret, len, &cp_len, &free_result TSRMLS_CC);

        if (cp_ret) {
            RETVAL_STRINGL((char *)cp_ret, cp_len, 1);
        } else {
            php_error(E_WARNING, "%s::%s(): could not convert return value from UTF-8", get_active_class_name(NULL TSRMLS_CC), get_active_function_name(TSRMLS_C));
        }

        if (free_result)
            g_free(cp_ret);
        else
            RETVAL_STRINGL((char *)ichar, len, 1);
    } else {
        RETVAL_NULL();
    }

    efree(ret);
}

%%
override gtk_source_language_get_mime_types
PHP_METHOD
{
    GSList *list;

    NOT_STATIC_METHOD();

    if (!php_gtk_parse_args(ZEND_NUM_ARGS(), "")) {
        return;
    }

    array_init(return_value);
    for (list = gtk_source_language_get_mime_types(GTK_SOURCE_LANGUAGE(PHPG_GOBJECT(this_ptr))); list; list = list->next) {
        add_next_index_string(return_value, (char *)list->data, 1);
        g_free(list->data);
    }

    g_slist_free(list);
}

%%
override gtk_source_language_get_tags
PHP_METHOD
{
    GSList *tags;
    zval *item;

    NOT_STATIC_METHOD();

    if (!php_gtk_parse_args(ZEND_NUM_ARGS(), "")) {
        return;
    }

    array_init(return_value);
    for (tags = gtk_source_language_get_tags(GTK_SOURCE_LANGUAGE(PHPG_GOBJECT(this_ptr))); tags; tags = tags->next) {
        MAKE_STD_ZVAL(item);
        phpg_gobject_new(&item, G_OBJECT(tags->data) TSRMLS_CC);
        add_next_index_zval(return_value, item);
    }
}

%% }}}
%% {{{ GtkSourceStyleScheme

%%
add-arginfo GtkSourceStyleScheme get_style_names
PHPGTK_ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO_EX(ARGINFO_NAME, 0, 0, 0)
    ZEND_ARG_INFO(0, convert)
ZEND_END_ARG_INFO();

%%
override gtk_source_style_scheme_get_style_names
PHP_METHOD
{
    GSList *schemes;
    gchar *fn = NULL;
    gchar *cp = NULL;
    gsize cp_len = 0;
    zend_bool free_cp = FALSE;
    zend_bool convert = 1;

    NOT_STATIC_METHOD();

    if (!php_gtk_parse_args(ZEND_NUM_ARGS(), "|b", &convert)) {
        return;
    }

    array_init(return_value);
    for (schemes = gtk_source_style_scheme_get_style_names(GTK_SOURCE_STYLE_SCHEME(PHPG_GOBJECT(this_ptr))); schemes; schemes = schemes->next) {
        if (convert) {
            fn = g_filename_to_utf8((char *) schemes->data, strlen((char *) schemes->data), NULL, NULL, NULL);
            cp = phpg_from_utf8(fn, strlen(fn), &cp_len, &free_cp TSRMLS_CC);
            if (cp) {
                add_next_index_string(return_value, (char *)cp, 1);
            } else {
                php_error(E_WARNING, "%s::%s(): could not convert return value from UTF-8", get_active_class_name(NULL TSRMLS_CC), get_active_function_name(TSRMLS_C));
            }
            if (free_cp)
                g_free(cp);
        } else {
            add_next_index_string(return_value, (char *) schemes->data, 1);
        }
        g_free(schemes->data);
    }
    g_slist_free(schemes);
}

%%}}}

%% {{{ GtkSourceView

%%
override gtk_source_iter_forward_search
PHP_METHOD
{
	zval *php_iter = NULL, *php_limit = NULL;
	zval *php_start = NULL, *php_stop = NULL;
	gchar *str = NULL;
	gint flags;
	GtkTextIter start, stop, *limit;
	gboolean found;

	NOT_STATIC_METHOD();

	if(!php_gtk_parse_args(ZEND_NUM_ARGS(),"Osi|N",
		&php_iter, gboxed_ce,
		&str,
		&flags,
		&php_limit, gboxed_ce)) {
		
		return;
	}
	
	if(php_limit) {
		limit = (GtkTextIter *)PHPG_GBOXED(php_limit);
	} else {
		limit = NULL;
	}

	found = gtk_source_iter_forward_search(
		(GtkTextIter *)PHPG_GBOXED(php_iter),
		str,
		flags,
		&start,
		&stop,
		limit
	);
	
	if(found) {
		phpg_gboxed_new(&php_start, GTK_TYPE_TEXT_ITER, &start, TRUE, TRUE TSRMLS_CC);
		phpg_gboxed_new(&php_stop, GTK_TYPE_TEXT_ITER, &stop, TRUE, TRUE TSRMLS_CC);
		php_gtk_build_value(&return_value,"(NN)",php_start,php_stop);
	} else {
		php_gtk_build_value(&return_value,"b",FALSE);
	}
}

%%
override gtk_source_iter_backward_search
PHP_METHOD
{
	zval *php_iter = NULL, *php_limit = NULL;
	zval *php_start = NULL, *php_stop = NULL;
	gchar *str = NULL;
	gint flags;
	GtkTextIter start, stop, *limit;
	gboolean found;

	NOT_STATIC_METHOD();

	if(!php_gtk_parse_args(ZEND_NUM_ARGS(),"Osi|N",
		&php_iter, gboxed_ce,
		&str,
		&flags,
		&php_limit, gboxed_ce)) {
		
		return;
	}
	
	if(php_limit) {
		limit = (GtkTextIter *)PHPG_GBOXED(php_limit);
	} else {
		limit = NULL;
	}

	found = gtk_source_iter_backward_search(
		(GtkTextIter *)PHPG_GBOXED(php_iter),
		str,
		flags,
		&start,
		&stop,
		limit
	);
	
	if(found) {
		phpg_gboxed_new(&php_start, GTK_TYPE_TEXT_ITER, &start, TRUE, TRUE TSRMLS_CC);
		phpg_gboxed_new(&php_stop, GTK_TYPE_TEXT_ITER, &stop, TRUE, TRUE TSRMLS_CC);
		php_gtk_build_value(&return_value,"(NN)",php_start,php_stop);
	} else {
		php_gtk_build_value(&return_value,"b",FALSE);
	}
}

%%}}}
